package com.hyuk.hyukfgw.biz.redis;

import com.hyuk.hyukfgw.framework.cache.redis.AbstractRedisListener;
import com.hyuk.hyukfgw.framework.cache.redis.RedisListener;
import com.hyuk.hyukfgw.framework.cache.util.EhcacheUtils;
import com.hyuk.hyukfgw.service.token.bean.RedisTokenMsg;
import com.hyuk.hyukfgw.service.token.bean.TokenInfo;
import com.hyuk.hyukfgw.util.copy.ShallowCopyUtils;
import com.hyuk.hyukfgw.util.enums.redis.RedisTopicEnum;
import com.hyuk.hyukfgw.util.json.JacksonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class TokenInfoRedisListener extends AbstractRedisListener<RedisTokenMsg> implements RedisListener {
    
    public static final String CACHE_TOKEN_PREFIX = "hyukfgw:access_token:MC31:";
    
    
    @Override
    public void doRecvMsg(RedisTokenMsg msgObject) {
        TokenInfo tokenInfo = ShallowCopyUtils.copyBeanProperties(msgObject, new TokenInfo());
        EhcacheUtils.set(CACHE_TOKEN_PREFIX + msgObject.getAccessToken(), JacksonUtils.toJson(tokenInfo));
    }
    
    @Override
    public MessageListener getMessageListener() {
        return this;
    }
    
    @Override
    public String getTopicName() { return RedisTopicEnum.ACCESS_TOKEN.getCode(); }
}
